/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package com.as.struts.form;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;

public class Form extends LazyValidatorForm {
    /**
         * Returns the value of <code>key</code>.  Return value may be null.
         *
         * @param key
         *
         * @return String
         */
    public String getString(String key) {
        return (String)get(key);
    }

    /**
         * Returns the value of the <code>key</code> as a java.sql.Date
         *
         * @param key
         *
         * @return java.sql.Date
         */
    public Date getSqlDate(String key) {
        Object o = get(key);
        if(o == null) {
            return null;
        }
        if(o instanceof Date) {
            return (Date)o;
        } else if(o instanceof String) {
            return Date.valueOf((String)o);
        } else {
            throw new RuntimeException("Object for key '" + key
                    + "' is of type '" +
                    o.getClass().getName() + "', not java.sql.Date.");
        }
    }

    /**
         * Returns the value of <code>key</code>, or <code>defaultValue</code> if
         * the value is null.
         *
         * @param key
         * @param defaultValue
         *
         * @return String
         */
    public String getString(String key, String defaultValue) {
        if(get(key) == null) {
            return defaultValue;
        } else {
            return (String)get(key);
        }
    }

    /**
         * Returns the value of a mapped or internal property.
         *
         * @param name
         * @param key
         *
         * @return Object
         */
    public Object get(String name, String key) {
        return dynaBean.get(name, key);
    }

    public Integer getInteger(String key) {
        Object o = get(key);
        if(o == null) {
            return null;
        }
        if(o instanceof Integer) {
            return (Integer)o;
        } else if(o instanceof String) {
            return new Integer((String)o);
        } else {
            throw new RuntimeException("Object for key '" + key
                    + "' is of type '" +
                    o.getClass().getName() + "', not Integer.");
        }
    }

    public Integer getInteger(String key, Integer defaultValue) {
        if(getString(key) == null) {
            return defaultValue;
        } else {
            return getInteger(key);
        }
    }

    /**
         * Shorthand method that calls <code>getLong("id")</code>.
         *
         * @return Long
         */
    public Long getId() {
        return getLong("id");
    }

    public Long getLong(String key) {
        Object o = get(key);
        if(o == null) {
            return null;
        }
        if(o instanceof Long) {
            return (Long)o;
        } else if(o instanceof String) {
            String value = (String)o;
            if("".equals(value.trim())) {
                return null;
            }
            return new Long((String)o);
        } else {
            throw new RuntimeException("Object for key '" + key
                    + "' is of type '" +
                    o.getClass().getName() + "', not Long.");
        }
    }

    public Long getLong(String key, Long defaultValue) {
        if(get(key) == null) {
            return defaultValue;
        } else {
            return getLong(key);
        }
    }

    public Boolean getBoolean(String key) {
        Object o = get(key);
        if(o == null) {
            return null;
        }
        if(o instanceof Boolean) {
            return (Boolean)o;
        } else if(o instanceof String) {
            String value = (String)o;
            if(value.trim().length() != 0) {
                return new Boolean((String)o);
            } else {
                return null;
            }
        } else {
            throw new RuntimeException("Object for key '" + key
                    + "' is of type '" +
                    o.getClass().getName() + "', not Boolean.");
        }
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        if(get(key) == null) {
            return defaultValue;
        } else {
            return getBoolean(key);
        }
    }

    public Boolean getBoolean(String key, boolean defaultValue) {
        if(get(key) == null) {
            return new Boolean(defaultValue);
        } else {
            return getBoolean(key);
        }
    }

    /**
         * Sets the value of a mapped or internal property with given value.
         *
         * @param name
         * @param key
         * @param value
         */
    public void set(String name, String key, Object value) {
        dynaBean.set(name, key, value);
    }

    /**
     Empty implementation.
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    }

}
